import 'package:app/common/save_update.dart';
import 'package:app/model/product_view_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'global.dart';

class ProductCard extends StatelessWidget {
  final ProductViewModel data;

  //final ValueChanged<bool> addCount;
  //final int shopCartCount;

  const ProductCard({
    Key key,
    this.data,
    //this.shopCartCount,
    //this.addCount,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(15),
      child: Row(
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(4),
            child: Image.asset(
              "assets/201905141517518.png",
              width: 40,
              height: 40,
            ),
          ),
          Padding(padding: EdgeInsets.only(left: 15)),
          Expanded(
              child: Column(
                  //padding: EdgeInsets.only(top: 5),
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Expanded(
                      child: Consumer<GlobalData>(
                          builder: (context, globalData, _) => Text(
                                this.data.productName,
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                  color: globalData.currentTheme,
                                ),
                              )),
                    )
                  ],
                ),
                Padding(padding: EdgeInsets.only(top: 5)),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  //EdgeInsets.all(10.0),
                  children: <Widget>[
                    Text(
                      "代理价: " + this.data.priceBig,
                      //maxLines: 12,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 14,
                        color: Color(0xFF999999),
                      ),
                    ),
                    Padding(padding: EdgeInsets.only(left: 45)),
                    Text(
                      "售价: " + this.data.priceSale,
                      //maxLines: 12,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 14,
                        color: Color(0xFF999999),
                      ),
                    ),
                  ],
                ),
              ])),
          Consumer<GlobalData>(
            builder: (context, globalData, _) => IconButton(
                icon: Icon(
                  Icons.add_shopping_cart,
                  color: globalData.currentTheme,
                ),
                onPressed: () {
                  //Provider.of<GlobalData>(context).listProductAdd();
                  Provider.of<GlobalData>(context).addShopCartCount(data);
                }),
          ),
          Consumer<GlobalData>(
              builder: (context, globalData, _) => PopupMenuButton<String>(
                  padding: EdgeInsets.only(left: 0),
                  offset: Offset(0, 50),
                  //color:Colors.pinkAccent,
                  icon: Icon(Icons.more_vert, color: globalData.currentTheme),
                  //onSelected: ,
                  //onSelected: handleDemoAction,
                  itemBuilder: (BuildContext context) => Global.actions.keys
                      .map((x) => PopupMenuItem(
                            child: Text(x),
                            value: x,
                          ))
                      .toList(),
                  //onSelected: Global.changeTheme,
                  onSelected: (x) async {
                    if ("修改" == x) {
                      String result = await Navigator.push(context,
                          MaterialPageRoute(builder: (context) {
                        return SaveUpdatePage(
                          data: data,
                          action: "修改",
                        );
                      }));
                      if (result != null) {
                        //重新加载列表数据
                        Provider.of<MainList>(context).reloadByType(result);
                      }
                    }
                    if ("删除" == x) {
                      await Dio().delete("http://106.54.126.84:8099/wx/delete/"+data.id.toString());
                      //重新加载列表数据
                      Provider.of<MainList>(context).reloadByType(data.productType);
                    }
                  }))
        ],
      ),
    );
  }

}
